package com.tim.bos;

import java.io.Serializable;
import java.util.Properties;
import java.util.UUID;

import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.Configurable;
import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.type.Type;

import com.tim.bos.Bostype;
import com.tim.bos.ICoreObject;

public class PersistantEntityIdGenerator  implements PersistentIdentifierGenerator, Configurable {
	
	public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
	      
	          if(object instanceof ICoreObject){
	        	Bostype bostype=object.getClass().getAnnotation(Bostype.class);
	        	String bt=bostype.value();
				  IBostype ibt=new MyBostype(bt);
	        	return BostypeUtils.getBostypeid(ibt);
	          }else{
	        	  throw new RuntimeException("Can't generate id and persistent for those NOT ICoreObject!!!");
	          }
	}
	
	
	 

	@Override
	public void configure(Type type, Properties params, Dialect d)
			throws MappingException {
	}


	@Override
	public String[] sqlCreateStrings(Dialect dialect) throws HibernateException {
		return new String[]{};
	}


	@Override
	public String[] sqlDropStrings(Dialect dialect) throws HibernateException {
		return new String[]{};
	}


	@Override
	public Object generatorKey() {
		return "bostypeidgenerator";
	}


}
